/*
 * @Author: WuFeng <763467339@qq.com>
 * @Date: 2024-10-23 11:17:13
 * @LastEditTime: 2024-11-13 10:58:26
 * @LastEditors: WuFeng <763467339@qq.com>
 * @Description: 
 * @FilePath: \markdown-magic\src\utils\windows\actions.js
 * @Copyright 版权声明
 */
import { emit } from '@tauri-apps/api/event'
import { message as messageAnt } from 'ant-design-vue'
import Windows from './index.js'

// 实例化 多窗口封装管理
let curWin = null
if (curWin === null) {
  curWin = new Windows()
  curWin.listen()
}
export default {
  curWin
}

/**
 * @desc 创建新窗口
 * @param args {object} {label: 'new', url: '/new', width: 500, height: 300, ...}
 */
export async function createWin(args) {
  await emit('win-create', args)
}

/**
 * @desc 新建 Markdown 窗口
 * @param args {object} {label: 'new', url: '/new', width: 500, height: 300,...}
 */
export async function createMarkdownWin() {
  const allWin = await curWin.getAllWin()
  // TODO: 单窗口模式
  const ref = allWin.some((item) => {
    return item.label === 'markdownWin1'
  })
  if (ref) {
    messageAnt.warning(`【Markdown 编辑器】窗口已存在`)
  } else {
    await createWin({
      label: `markdownWin1`,
      title: `Markdown 编辑器`,
      url: '/markdown-magic',
      width: 800,
      height: 800,
      minWidth: 800,
      minHeight: 800,
      visible: true,
      closable: true,
      maximizable: true,
      decorations: true,
      maximized: false,
      // parent: 'main'
    })
  }
  // TODO: 多窗口模式
  // if (allWin.length >= 10) {
  //   messageAnt.warning('最多可新建9个窗口')
  // } else {
  //   await createWin({
  //     // label: `markdownWin`,
  //     // title: `自定义窗口${allWin.length}`,
  //     label: `markdownWin1`,
  //     title: `Markdown 窗口`,
  //     url: '/markdown-magic',
        // width: 800,
        // height: 800,
        // minWidth: 800,
        // minHeight: 800,
  //     visible: true,
  //     closable: true,
  //     maximizable: true,
  //     decorations: true,
  //     maximized: false,
  //     // parent: 'main'
  //   })
  // }
}

/**
 * @desc 通用窗口
 */
export async function createCommonWin(args) {
  const allWin = await curWin.getAllWin()
  console.log(`🚀 ~ createCommonWin ~ allWin:`, allWin)
  // let currentWindow = null
  const ref = allWin.some(item => item.label === args?.label)
  // const ref = allWin.some(function (item) {
  //   // currentWindow = Object.getPrototypeOf(item)
  //   // currentWindow = await curWin.getWin('main')
  //   return item.label === args?.label
  // })
  // console.log(`🚀 ~ ref ~ currentWindow:`, currentWindow)
  if (ref) {
    // await currentWindow.setTitle('xxxxx')
    // console.log(`🚀 ~ createCommonWin ~ await currentWindow.title():`, await currentWindow.title())
    messageAnt.warning(`【${args.title}】窗口已存在`)
  } else {
    await createWin({
      label: args?.label??'/',
      title: args?.title??'/',
      url: args?.url??'/',
      width: 800,
      height: 800,
      minWidth: 800,
      minHeight: 800,
      visible: true,
      closable: true,
      maximizable: true,
      decorations: true,
      maximized: false,
      // parent: 'main'
    })
  }
}

/**
 * @desc 登录窗口
 */
export async function loginWin() {
  await createWin({
    label: 'main_login',
    title: '登录',
    url: '/login',
    width: 400,
    height: 320,
    resizable: false,
    alwaysOnTop: true
  })
}

export async function aboutWindow() {
  await createWin({
    label: 'about',
    title: '关于',
    url: '/about',
    width: 450,
    height: 360,
  })
}